import { followDoctor } from '@/services/consult'
import { ref } from 'vue'
import type { FollowType } from '@/types/consult'
// ! 关注
export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  // 加载中
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      // 关注成功，后台服务器关注成功
      await followDoctor(item.id, type)
      // 本地数据的状态需要同步
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      // finally不管成功还是失败，都会执行
      // 加载完成
      loading.value = false
    }
  }
  return { loading, follow }
}
